package com.jinniulive.platform.module.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.R;
import com.jinniulive.platform.activity.MainActivity;
import com.jinniulive.platform.bean.CodeInfo;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.common.util.MD5;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class ResetPasswordActivity extends AppCompatActivity implements TextWatcher, View.OnFocusChangeListener {

    private EditText mEditTextPassword,mEditTextPassword2;
    private Button mButtonLogin;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        mPhone = getIntent().getStringExtra("phone");
        initView();
        setListener();
    }

    private void setListener() {
        mEditTextPassword.addTextChangedListener(this);
        mEditTextPassword2.addTextChangedListener(this);
        mEditTextPassword.setOnFocusChangeListener(this);

    }

    private void initView() {
        mEditTextPassword = (EditText) findViewById(R.id.activity_reset_password_et_password);
        mEditTextPassword2 = (EditText) findViewById(R.id.activity_reset_password_et_password2);
        mButtonLogin = (Button) findViewById(R.id.activity_reset_password_bt_login);


    }
    //判断字符串是否是 数字或字母
    public static boolean isLetterOrDigit(String str) {
        boolean isLetterOrDigit = false;//定义一个boolean值，用来表示是否包含字母或数字
        for (int i = 0; i < str.length(); i++) {
            if (Character.isLetterOrDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符
                isLetterOrDigit = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isLetterOrDigit && str.matches(regex);
        return isRight;
    }
    //登陆点击事件
    public void onClick(View view) {


        String passwork = mEditTextPassword.getText().toString().trim();
        final String passwork2 = mEditTextPassword2.getText().toString().trim();
        if (isLetterOrDigit(passwork2)){
            if (passwork2.length()<6||passwork2.length()>16){
                Toast.makeText(ResetPasswordActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
            }else{
                final String md5Password = MD5.md5(passwork2);
                if (passwork.equals(passwork2)){
                    String format = String.format(NetConfig.CZMM, mPhone, md5Password);
                    RequestParams params =new RequestParams(format);
                    x.http().get(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            Gson gson = new Gson();
                            CodeInfo codeInfo = gson.fromJson(result, CodeInfo.class);
                            int errorcode = codeInfo.getErrorcode();
                            String codedes = codeInfo.getCodedes();
                            if (errorcode == 200 ){
                                SharedPreferences sp = getSharedPreferences("autoLogin", MODE_PRIVATE);
                                SharedPreferences.Editor edit=sp.edit();
                                edit.putString("account",mPhone);
                                edit.putString("password",passwork2);
                                edit.commit();
                                startActivity(new Intent(ResetPasswordActivity.this, MainActivity.class));
                                finish();
                            }else {
                                Toast.makeText(ResetPasswordActivity.this, codedes, Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                }else{
                    Toast.makeText(this, "俩次输入的密码不同请重新输入", Toast.LENGTH_SHORT).show();
                }

            }
        }else{
            Toast.makeText(ResetPasswordActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (TextUtils.isEmpty(mEditTextPassword.getText().toString().trim())){
            mEditTextPassword2.setEnabled(false);
        }else{
            mEditTextPassword2.setEnabled(true);
        }
        if (TextUtils.isEmpty(mEditTextPassword2.getText().toString().trim())){
            mButtonLogin.setEnabled(false);
            mButtonLogin.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
        }else {
            mButtonLogin.setEnabled(true);
            mButtonLogin.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_14));
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            //得到焦点
        }else{
            //失去
            switch (v.getId()){
                case R.id.activity_reset_password_et_password:
                    String password = mEditTextPassword.getText().toString().trim();
                    if (isLetterOrDigit(password)){
                        if (password.length()<6){
                            Toast.makeText(ResetPasswordActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(ResetPasswordActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }

        }

    }
}
